package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public int save(Comment comment) {
        //1.往comment表中插入记录
        mongoTemplate.save(comment);

        //2.根据操作的类型修改指定的数量
        switch (comment.getCommentType()) {
            case 1: {//对动态点赞
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setLikeCount(movement.getLikeCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }

            case 2: {//对动态评论
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setCommentCount(movement.getCommentCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }

            case 3: {//对动态喜欢
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改对应的数量 ++
                movement.setLoveCount(movement.getLoveCount() + 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }

            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public int delete(Long userId, ObjectId publishId, Integer commentType) {
        //1.从comment表中删除一条记录
        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(commentType).and("userId").is(userId));
        mongoTemplate.remove(query,Comment.class);

        //2.根据操作的类型修改指定的数量
        switch (commentType){
            case 1:{ //对动态取消点赞
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setLikeCount(movement.getLikeCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }

            case 2:{ //对动态取消评论
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setCommentCount(movement.getCommentCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }

            case 3:{ //对动态取消喜欢
                //2.1 查询被操作的对象
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改对应的数量 --
                movement.setLoveCount(movement.getLoveCount() - 1);
                //2.3 将数据更新到mongo中
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }

            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public PageBeanVo findByPage(ObjectId publishId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象query(动态id,评论,分页,排序)
        Query query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(2))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.分页查询
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.查询满足条件的总记录数
        query = new Query(Criteria.where("publishId").is(publishId).and("commentType").is(2));
        long count = mongoTemplate.count(query, Comment.class);
        //4.封装pageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }

    @Override
    public PageBeanVo findByPage(Long publishUserId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(publishiUserId,评论类型,排序,分页)
        Query query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.查询comment表
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.查询总记录数
        query = new Query(Criteria.where("publishUserId").is(publishUserId).and("commentType").is(commentType));
        long count = mongoTemplate.count(query, Comment.class);

        //4.封装pageBeanVo返回
        return new PageBeanVo(pageNum,pageSize,count,comments);
    }
}
